package cn.tristenstudy.blog.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author xichen1.li
 * @date 2020-10-29 19:24
 */
@Configuration
public class ShiroConfig {


    /**
     * ShiroFilterFactoryBean
     *
     * @return ShiroFilterFactoryBean
     */
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(securityManager);

        //添加shiro的内置过滤器
        /*
         * anno: 无需认证就可以访问
         * authc: 必须认证了才能访问
         * user: 必须拥有 记住我的功能才能访问
         * perms: 拥有对某个资源的权限才能访问
         * role: 拥有某个角色权限才能访问
         */
        Map<String, String> filterMap = new LinkedHashMap<>();

        //授权,正常情况下，没有授权会跳转到未授权页面
        filterMap.put("/admin/blog/*", "perms[user:admin]");
        filterMap.put("/admin/type/*", "perms[user:admin]");
        filterMap.put("/admin/*", "authc");
        bean.setFilterChainDefinitionMap(filterMap);
        //设置的登录请求
        bean.setLoginUrl("/user/toLogin");
        //设置未授权请求
        bean.setUnauthorizedUrl("/user/unauthorized");
        return bean;
    }


    /**
     * DefaultWebSecurityManager
     * * @return securityManager
     */
    @Bean("securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联Realm
        securityManager.setRealm(userRealm);
        return securityManager;
    }


    /**
     * 创建 Realm对象,需要自定义
     *
     * @return Realm对象
     */
    @Bean("userRealm")
    public UserRealm getUserRealm() {
        UserRealm userRealm = new UserRealm();
        //修改密码加密方式
        userRealm.setCredentialsMatcher(hashedCredentialsMatcher());
        return userRealm;
    }

    /**
     * 密码加密算法设置
     * @return HashedCredentialsMatcher
     */
    @Bean("hashedCredentialsMatcher")
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        //散列的次数
        hashedCredentialsMatcher.setHashIterations(2);
        return hashedCredentialsMatcher;
    }


    /**
     * 整合ShiroDialect：用来整合 shiro thyemleaf
     *
     * @return ShiroDialect
     */
    @Bean
    public ShiroDialect getShiroDialect() {
        return new ShiroDialect();
    }


}
